package com.giw.project.file_manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class SplitFileByChar {
	private final static File fInput = new File(
			"/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split");
	private String out_path = "/home/giorgio/Scrivania/GIW/progetto/0-grafo/Split/newSplit";
	
	//ricorda che type deve essere maiuscolo!!!
	public void run(String type) {
		// sottocartella tipo
		out_path += "/"+type;
		makeDir(out_path);
		
		try {
			FileInputStream fisInput = new FileInputStream(fInput+"/"+type+".txt");
			InputStreamReader isrInput = new InputStreamReader(fisInput);
			BufferedReader brInput = new BufferedReader(isrInput);
			
			String lineaInput = brInput.readLine();
			
			while (lineaInput != null) {

				String currentNode = lineaInput.split("\t")[0].toLowerCase();
				
				// trasformo in stringa che è più comodo
				char[] tmp_char = new char[1]; 
				tmp_char[0] = giveFirstValidChar(currentNode);
				String first_char = new String(tmp_char);
				
				//lo riconosce?
				if (!first_char.equals("\"")&&!first_char.equals("(")){
					String dirName = out_path+"/"+first_char;
					String fileName = dirName+"/"+type+"_"+first_char;

					//creazione cartella, se esiste già non la crea
					makeDir(dirName);
								
				String new_char = first_char;
				while (lineaInput != null && new_char.equals(first_char)) {
					FileWriter fw = new FileWriter(fileName+".txt",true);
					PrintWriter pw = new PrintWriter(fw);
					
					pw.println(lineaInput);
					pw.close();
					
					if (lineaInput!=null) {
						lineaInput = brInput.readLine();
						currentNode = lineaInput.split("\t")[0].toLowerCase();
						
						// trasformo in stringa che è più comodo
						tmp_char = new char[1]; 
						tmp_char[0] = giveFirstValidChar(currentNode);
						first_char = new String(tmp_char);
						
						if (!first_char.equals("\"")&&!first_char.equals("(")){
							dirName = out_path+"/"+first_char;
							fileName = dirName+"/"+type+"_"+first_char;
						}else{
							lineaInput = brInput.readLine();
							//invochiamo metodo apposito
						}
						fw.close();
					}
				}
				}else{
					lineaInput = brInput.readLine();
					//invochiamo metodo apposito
				}
			}

			brInput.close();
			isrInput.close();
			fisInput.close();
			

		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	
	private static void makeDir(String dirName) {
		boolean success = (new File(dirName).mkdir());
		if(success) {
			System.out.println(dirName + " -> created");
		} else {
			System.out.println("Impossible to create -> "+dirName);
		}
	}
	
	// devono essere passate stringhe lowercase
	private static char giveFirstValidChar(String input) {
		System.out.println(input);
		int i = 0;
		boolean flag = true;
		char i_char = '-'; //carattere di default
		while (i<input.length() && flag) {
			i_char = input.charAt(i);
			//i_char!='\"' && i_char!=' ' && i_char!='*'&& i_char!='/'
			if (Character.isLetter(i_char)||isNumber(i_char)) {
				flag=false;
			}else if(i_char=='(') {
				return i_char; //valore non valido, vuol dire che non c'è il litterale ma solo il tipo
			}
			i++;
		}
		System.out.println(i_char);
		return i_char;
	}
	
	private static boolean isNumber(char input) {
		if (input=='1'||input=='2'||input=='3'||input=='4'||input=='5'||
			input=='6'||input=='7'||input=='8'||input=='9'||input=='0')
			return true;
		return false;
	}

	public static void main(String[] args) throws FileNotFoundException {
		SplitFileByChar s = new SplitFileByChar();
		s.run("litteral"); // si fa cos�?
	}
}
